package com.example.vehiclemanagesystem.controller;

import com.example.vehiclemanagesystem.entity.Driver;
import com.example.vehiclemanagesystem.entity.Route;
import com.example.vehiclemanagesystem.entity.Vehicle;
import com.example.vehiclemanagesystem.service.DriverService;
import com.example.vehiclemanagesystem.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    @PostMapping("/addVehicle")
    public Map<String, Object> addVehicle(@RequestBody Map<String, String> remap) {
        Integer v_id = Integer.parseInt(remap.get("v_id"));
        String license = remap.get("license");
        Integer capacity = Integer.parseInt(remap.get("capacity"));
        Double meters = Double.parseDouble(remap.get("meters"));
        String maintenance = remap.get("maintenance");
        Map<String, Object> map = new HashMap<>();
        try {
            Vehicle vehicle = new Vehicle(v_id, license, capacity, meters, maintenance);
            Vehicle vehicle1 = vehicleService.selectVehicleByVehicleID(v_id);
            if (vehicle1 != null) {
                map.put("success", false);
                map.put("message", "车辆已存在");
            } else {
                vehicleService.addNewVehicle(vehicle);
                map.put("success", true);
                map.put("message", "添加成功 ");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "添加失败！");
        }
        return map;
    }
/*
    @RequestMapping("/allDriver")
    public Map<String,Object> PRINTALL(){
        List<Driver> driverList = driverService.allDriver();
        Map<String, Object> map = new HashMap<>();
        map.put("整个Driver表",driverList);
        return map;
    }*/

    @PostMapping("/deleteVehicle")
    public Map<String, Object> deleteVehicle(@RequestBody String input) {
        input = input.substring(0,input.length()-1);
        Integer v_id = Integer.parseInt(input);
        Map<String, Object> map = new HashMap<>();
        try {
            Vehicle vehicle = vehicleService.selectVehicleByVehicleID(v_id);
            if (vehicle == null) {
                map.put("success", false);
                map.put("message", "车辆不存在");
            } else {
                vehicleService.deleteVehicle(vehicle);
                map.put("success", true);
                map.put("message",/*d_id+*/"车辆删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }

    @RequestMapping("/allVehicle")
    public Map<String, Object> printVehicle() {
        List<Vehicle> vehicleList = vehicleService.allVehicle();
        Map<String, Object> map = new HashMap<>();
        map.put("pagenum", 1);
        map.put("pagesize", 1);
        map.put("整个车辆表", vehicleList);
        return map;
    }

    @RequestMapping("/searchVehicle")
//    public Map<String,Object> thisVehicle(@RequestBody Map<String,String>remap){
    public Map<String, Object> thisVehicle(@RequestParam("query") String input) {
//        String input=remap.get("query");
        Map<String, Object> map = new HashMap<>();
        if (input == null || input.equals("")) {
            List<Vehicle> vehicleList = vehicleService.allVehicle();
            //Map<String, Object> map = new HashMap<>();
            map.put("pagenum", 1);
            int total = vehicleList.size();
            map.put("total", total);
            map.put("success", true);
            map.put("整个车辆表", vehicleList);
            return map;
        }
//        Integer v_id = Integer.parseInt(remap.get("Query"));
        Integer v_id;
        try {
            v_id = Integer.parseInt(input);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "输入不合法");
            return map;
        }

        Vehicle vehicle = vehicleService.selectVehicleByVehicleID(v_id);
        ArrayList<Vehicle> vehicles = new ArrayList<>();
        vehicles.add(vehicle);
        if (vehicle == null) {
            map.put("success", false);
            map.put("message", "车辆不存在");
        } else {
            map.put("success", true);
            map.put("message",/*d_id+*/"搜索成功");
            map.put("整个车辆表", vehicles);
        }
        return map;
    }

    @PostMapping("/updateVehicle")
    public Map<String, Object> updateVehicle(@RequestBody Map<String, String> remap) {
        Integer v_id = Integer.parseInt(remap.get("v_id"));
        String license = remap.get("license");
        Integer capacity = Integer.parseInt(remap.get("capacity"));
        Double meters = Double.parseDouble(remap.get("meters"));
        String maintenance = remap.get("maintenance");
        Map<String, Object> map = new HashMap<>();
        try {
            Vehicle vehicle1 = vehicleService.selectVehicleByVehicleID(v_id);
            if (vehicle1 == null) {
                map.put("success", false);
                map.put("message", "车辆不存在");
            } else {
                Vehicle vehicle = new Vehicle(v_id, license, capacity, meters, maintenance);
                vehicleService.updateVehicle(vehicle);
                map.put("success", true);
                map.put("message",/*d_id+*/"车辆修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }


    @RequestMapping("/getVehicleeList")
    public  List<Map<String,Object>> getVehicleeList(@RequestParam("query") String input) {
        List<Vehicle> vehicleList = vehicleService.allVehicle();
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (Vehicle vehicle : vehicleList){
            Map<String,Object> map = new HashMap<>();
            map.put("license",vehicle.getLicense());
            map.put("v_id",vehicle.getV_id());
            //map.put("type","vehicle");
            mapList.add(map);
        }
        return mapList;
    }
}
